<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>职级管理</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .cont {
            padding: 10px;
        }

        #xz {
            margin-bottom: 10px;
        }

        #tbody td>button:first-child,
        #tbody td>button:nth-child(2) {
            margin-right: 10px;
        }

        #tbody td {
            line-height: 2.42857143;
        }

        #tc {
            position: absolute;
            padding: 10px;
            width: calc(100vw - 20px);
            height: 100vh;
            top: 0px;
            background-color: white;
            display: none;
        }

        #tc>button {
            margin-right: 10px;
        }
    </style>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css"
        integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"
        integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
        crossorigin="anonymous"></script>
</head>

<body>
    <div class="cont">
        <button class="btn btn-primary m-r-5 mbtm" id="xz" onclick="tC()" value="31">新增职级</button>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th class="text-center">职级</th>
                    <th class="text-center" id="cZ">操作</th>
                </tr>
            </thead>
            <tbody class="text-center" id="tbody">
            </tbody>
            <tfoot class="text-center">
                <tr>
                    <td colspan="5">
                        <nav aria-label="Page navigation">
                            <ul class="pagination">
                                <li onclick="syy()">
                                    <a href="#" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                                <li><a href="#" id="yema">1</a></li>
                                <li onclick="xyy()">
                                    <a href="#" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                            </ul>
                        </nav>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
    <div id="tc">
        <div class="h3" id="mc">新增职级</div>
        <div class="form-group">
            <label for="exampleInputEmail1">职级名称：</label>
            <input type="text" class="form-control" placeholder="职级名称" id="mingC">
        </div>
        <div class="h4">职级权限：</div>
        <p id="qx">

        </p>
        <button class="btn btn-primary m-r-5" onclick="xZ()" id="anmc">新增</button><button class="btn btn-primary m-r-5"
            onclick="qX()">取消</button>
    </div>
    <script src="../js/pubilc.js"></script>
    <script>
        var BG = []
        var fly = -1
        var item = 1
        var y = 1
        var name = ''
        var _index
        var arr = []
        var arr1 = []
        var xuanzong = []
        bd()
        function bd() {
            xhr({
                methods: 'get',
                url: `/user/info?token=${sessionStorage.getItem('token')}`,
                success: function (res) {
                    if (res.role_id != null) {
                        xhr({
                            methods: 'post',
                            url: `/roleSys/getRoleList`,
                            data: JSON.stringify({
                                role_school_id: Number(sessionStorage.getItem('id'))
                            }),
                            success: function (fly) {
                                for (i = 0; i < fly.length; i++) {
                                    if (res.role_id == fly[i].role_id) {
                                        var index = fly[i].role_rank_id
                                        xhr({
                                            methods: 'get',
                                            url: `/rankSys/getRank?school_id=${Number(sessionStorage.getItem('id'))}`,
                                            success: function (l) {
                                                for (j = 0; j < l.length; j++) {
                                                    if (l[j].rank_id == index) {
                                                        var ind = xz.value.split('')[0]-1
                                                        var chaiji
                                                        if(l[j].rank_sys.length<ind){
                                                            xz.style.display = 'none'
                                                            cZ.style.display = 'none'
                                                            for (s = 0; s < yinC.length; s++) {
                                                                yinC[s].style.display = 'none'
                                                            }
                                                            for (i = 0; i < anN.length; i++) {
                                                                if (anN[i].value == 32) {
                                                                    anN[i].style.display = 'none'
                                                                }
                                                            }
                                                            for (j = 0; j < anN.length; j++) {
                                                                if (anN[j].value == 33) {
                                                                    anN[j].style.display = 'none'
                                                                }
                                                            }
                                                            return
                                                        }else{
                                                            chaiji = l[j].rank_sys[ind].page_function_ids.split(',')
                                                        }
                                                        console.log(chaiji)
                                                        if (chaiji.indexOf('31') == -1) {
                                                            xz.style.display = 'none'
                                                        }
                                                        if (chaiji.indexOf('32') == -1 && chaiji.indexOf('33') == -1) {
                                                            cZ.style.display = 'none'
                                                            for (s = 0; s < yinC.length; s++) {
                                                                yinC[s].style.display = 'none'
                                                            }
                                                        }
                                                        if (chaiji.indexOf('32') == -1) {
                                                            for (i = 0; i < anN.length; i++) {
                                                                if (anN[i].value == 32) {
                                                                    anN[i].style.display = 'none'
                                                                }
                                                            }
                                                        }
                                                        if (chaiji.indexOf('33') == -1) {
                                                            for (j = 0; j < anN.length; j++) {
                                                                if (anN[j].value == 33) {
                                                                    anN[j].style.display = 'none'
                                                                }
                                                            }
                                                        }
                                                    }

                                                }

                                            }
                                        })
                                    }
                                }
                            }
                        })
                    } else {
                        return
                    }
                }
            })
        }
        function oo(fly) {
            xhr({
                methods: 'get',
                url: `/rankSys/getFn?school_id=${Number(sessionStorage.getItem('id'))}`,
                success: function (res) {
                    console.log(res)
                    qx.innerHTML = ''
                    for (i = 0; i < res.length; i++) {
                        qx.innerHTML += `<div class="form-group">
                            <label for="exampleInputEmail1">${res[i].page_name}</label>
                            <div class="checkbox"></div>
                        </div>`
                        var checkbox = document.getElementsByClassName('checkbox')[i]
                        if (fly) {
                            for (j = 0; j < res[i].page_function.length; j++) {
                                checkbox.innerHTML += `<label><input type="checkbox" value="${res[i].page_function[j].function_id}" disabled> ${res[i].page_function[j].page_function_name}</label>`
                            }
                        } else {
                            for (j = 0; j < res[i].page_function.length; j++) {
                                checkbox.innerHTML += `<label><input type="checkbox" value="${res[i].page_function[j].function_id}"> ${res[i].page_function[j].page_function_name}</label>`
                            }
                        }


                    }
                }
            })
        }
        var ch = document.getElementsByTagName('input')
        var mc = document.getElementById('mc')
        function tC() {
            tc.style.display = 'block'
            mc.innerHTML = '新增职级'
            anmc.innerHTML = '新增'
            fly = -1
            oo()
        }
        function qX() {
            tc.style.display = 'none'
            mingC.value = ''
            for (i = 1; i < ch.length; i++) {
                ch[i].checked = false
            }
        }
        function xg(i) {
            fly = -2
            _index = i
            tc.style.display = 'block'
            mc.innerHTML = '修改职级'
            anmc.innerHTML = '修改'
            mingC.value = name
            anmc.disabled = false
            oo()
            xhr({
                methods: 'get',
                url: `/rankSys/getRankDetail?rank_id=${Number(i)}`,
                success: function (res) {
                    console.log(res)
                    mingC.value = res.rank_name
                    for (j = 0; j < res.rank_sys.length; j++) {
                        var chaiji = res.rank_sys[j].page_function_ids.split(',')
                        for (s = 1; s < ch.length; s++) {
                            for (ss = 0; ss < chaiji.length; ss++) {
                                if (chaiji[ss] == ch[s].value) {
                                    ch[s].checked = true
                                }
                            }
                        }
                    }
                }
            })
        }
        function xZ() {
            arr = []
            arr1 = []
            if (fly == -1) {
                if (mingC.value) {
                    for (q = 0; q < ch.length; q++) {
                        if (ch[q].checked == true) {
                            for (i = 1; i < ch.length; i++) {
                                if (ch[i].checked == true) {
                                    arr.push(ch[i].value.split('')[0])
                                    xuanzong.push(ch[i].value)
                                }
                            }
                            for (j = 0; j < arr.length; j++) {
                                if (arr[j] == arr[j + 1]) {
                                    continue
                                } else {
                                    arr1.push(arr[j])
                                }
                            }
                            var sys1 = []
                            for (h = 0; h < arr1.length; h++) {
                                var hh = []
                                var s = ''
                                for (hz = 0; hz < arr.length; hz++) {
                                    if (arr[hz].indexOf(arr1[h]) != -1) {
                                        hh.push(xuanzong[hz])
                                        s = arr1[h]
                                    }
                                }
                                sys1.push({
                                    page_id: Number(s),
                                    page_function_ids: hh
                                })
                            }
                            xhr({
                                methods: 'post',
                                url: '/rankSys/add',
                                data: JSON.stringify({
                                    rank_name: mingC.value,
                                    rank_school_id: Number(sessionStorage.getItem('id')),
                                    sys: sys1
                                }),
                                success: function (res) {
                                    console.log(res)
                                    biaoG()
                                    tc.style.display = 'none'
                                }
                            })
                        }
                    }
                } else {
                    alert('请填写职级名称')
                }
            } else if (fly == -2) {
                if (mingC.value) {
                    for (q = 0; q < ch.length; q++) {
                        if (ch[q].checked == true) {
                            for (i = 1; i < ch.length; i++) {
                                if (ch[i].checked == true) {
                                    arr.push(ch[i].value.split('')[0])
                                    xuanzong.push(ch[i].value)
                                }
                            }
                            for (j = 0; j < arr.length; j++) {
                                if (arr[j] == arr[j + 1]) {
                                    continue
                                } else {
                                    arr1.push(arr[j])
                                }
                            }
                            var sys1 = []
                            for (h = 0; h < arr1.length; h++) {
                                var hh = []
                                var s = ''
                                for (hz = 0; hz < arr.length; hz++) {
                                    if (arr[hz].indexOf(arr1[h]) != -1) {
                                        hh.push(xuanzong[hz])
                                        s = arr1[h]
                                    }
                                }
                                sys1.push({
                                    page_id: Number(s),
                                    page_function_ids: hh
                                })
                            }
                            xhr({
                                methods: 'post',
                                url: '/rankSys/editRank',
                                data: JSON.stringify({
                                    rank_id: Number(_index),
                                    rank_school_id: Number(sessionStorage.getItem('id')),
                                    sys: sys1
                                }),
                                success: function (res) {
                                    console.log(res)
                                    biaoG()
                                    tc.style.display = 'none'
                                }
                            })
                        }
                    }
                }
                mingC.value = ''
                for (i = 1; i < ch.length; i++) {
                    ch[i].checked = false
                }
            } else {
                alert('请输入职级名称')
            }
        }
        // function sc(i){
        //     arr.splice(i,1)
        //     biaoG()
        // }
        function ck(i) {
            fly = -3
            // _index = i
            tc.style.display = 'block'
            mc.innerHTML = '查看职级'
            anmc.innerHTML = '查看'
            oo(fly)
            ch[0].disabled = true
            anmc.disabled = true
            xhr({
                methods: 'get',
                url: `/rankSys/getRankDetail?rank_id=${Number(i)}`,
                success: function (res) {
                    console.log(res)
                    mingC.value = res.rank_name
                    for (j = 0; j < res.rank_sys.length; j++) {
                        var chaiji = res.rank_sys[j].page_function_ids.split(',')
                        for (s = 1; s < ch.length; s++) {
                            for (ss = 0; ss < chaiji.length; ss++) {
                                if (chaiji[ss] == ch[s].value) {
                                    ch[s].checked = true
                                }
                            }
                        }
                    }
                }
            })
        }
        biaoG()
        function biaoG() {
            xhr({
                methods: 'get',
                url: `/rankSys/getRank?school_id=${Number(sessionStorage.getItem('id'))}`,
                success: function (res) {
                    console.log(res)
                    tbody.innerHTML = ''
                    var ye = res.slice((item - 1) * 10, (item - 1) * 10 + Number(10))
                    yema.innerHTML = y
                    for (i = 0; i < ye.length; i++) {
                        tbody.innerHTML += `<tr>
                            <td>${ye[i].rank_name}</td>
                            <td class="yinC"><button class="btn btn-primary m-r-5" onclick='xg(${ye[i].rank_id})' value="32">修改</button><button class="btn btn-primary m-r-5" onclick='ck(${ye[i].rank_id})' value="33">查看</button></td>
                        </tr>`
                    }
                }
            })

        }
        var yinC = document.getElementsByClassName('yinC')
        var anN = document.getElementsByTagName('button')
        function syy() {
            if (item <= 1) {
                item = 1
                y = 1
                return
            }
            item--
            y--
            biaoG()
        }
        function xyy() {
            item++
            y++
            biaoG()
        }
    </script>
</body>

</html>